﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string page, string rows, string name)
        {
            int p = 0, r = 0;
            if (!string.IsNullOrEmpty(page))
            {
                p = int.Parse(page);
            }
            if (!string.IsNullOrEmpty(rows))
            {
                r = int.Parse(rows);
            }

            
            var rs = GetCustomer(p, r);
    
            if (!string.IsNullOrEmpty(name))
            {
                var rss = from c in rs where c.Name.Contains(name) select c;
                
                var result = new { total = rss.ToList().Count, rows = rss.ToList() };
                return Json(result, JsonRequestBehavior.AllowGet);

            }
            else
            {
                var result = new { total = rs.Count, rows = rs };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
           
        }

        public ActionResult Detail(string id)
        {
            Customer c = new Customer();
            if (!string.IsNullOrEmpty(id))
            {
                c.Id = int.Parse(id);
                c.Name = string.Format("客商{0}", id);
                c.Address = string.Format("地址{0}", id);
                c.RegNum = string.Format("注册号{0}", id);
                c.Business = string.Format("经营范围{0}",id);
                c.NcNum = string.Format("nc{0}",id);
                c.Status = "已审核";

            }

            return View(c);
        }

        private List<Customer> GetCustomer(int p,int r)
        {
            var t = new List<Customer>();

            int max = 100;
            int i = 0;

            if(p!=0&&r!=0)
            {
                i = p * r - r + 1;
                max = i + r;
            }

            for (int ii = i; ii < max; ii++)
            {
                t.Add(
                    new Customer
                    {
                        Id = ii,
                        Name = string.Format("客商{0}", ii),
                        Address = string.Format("地址{0}", ii),
                        RegNum = string.Format("注册号{0}", ii),
                        Business = string.Format("经营范围{0}",ii),
                        NcNum = string.Format("nc{0}",ii),
                        Status = "已审核"
                    });
            }

            

            return t;
        }
    }

   
}
